package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.vo.ChatPrivateSessionVO;
import com.zenithmind.chat.service.ChatPrivateSessionService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 私聊会话管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "私聊会话管理", description = "私聊会话相关接口")
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/chat/private-session")
@RequiredArgsConstructor
public class ChatPrivateSessionController {

    private final ChatPrivateSessionService privateSessionService;

    @Operation(summary = "分页查询用户会话列表")
    @GetMapping("/page")
    public Result<PageResult<ChatPrivateSessionVO>> getUserSessionPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        
        String userId = UserContext.getUserId();
        PageResult<ChatPrivateSessionVO> result = privateSessionService.getUserSessionPage(userId, pageNum, pageSize);
        
        return Result.success(result);
    }

    @Operation(summary = "查询用户会话列表")
    @GetMapping("/list")
    public Result<List<ChatPrivateSessionVO>> getUserSessions() {
        String userId = UserContext.getUserId();
        List<ChatPrivateSessionVO> result = privateSessionService.getUserSessions(userId);
        
        return Result.success(result);
    }

    @Operation(summary = "根据用户ID查询会话")
    @GetMapping("/by-user")
    public Result<ChatPrivateSessionVO> getSessionByUserIds(
            @Parameter(description = "对方用户ID") @RequestParam String otherUserId) {
        
        String userId = UserContext.getUserId();
        ChatPrivateSessionVO result = privateSessionService.getSessionByUserIds(userId, otherUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "创建或获取会话")
    @PostMapping("/create-or-get")
    public Result<String> createOrGetSession(
            @Parameter(description = "对方用户ID") @RequestParam String otherUserId) {
        
        String userId = UserContext.getUserId();
        String result = privateSessionService.createOrGetSession(userId, otherUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "更新会话最后消息信息")
    @PostMapping("/update-last-message")
    public Result<Boolean> updateLastMessage(
            @Parameter(description = "会话ID") @RequestParam String sessionId,
            @Parameter(description = "消息ID") @RequestParam String messageId,
            @Parameter(description = "消息内容") @RequestParam String messageContent) {
        
        Boolean result = privateSessionService.updateLastMessage(sessionId, messageId, messageContent);
        
        return Result.success(result);
    }

    @Operation(summary = "更新用户未读数")
    @PostMapping("/update-unread-count")
    public Result<Boolean> updateUserUnreadCount(
            @Parameter(description = "会话ID") @RequestParam String sessionId,
            @Parameter(description = "增量") @RequestParam Integer increment) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.updateUserUnreadCount(sessionId, userId, increment);
        
        return Result.success(result);
    }

    @Operation(summary = "清零用户未读数")
    @PostMapping("/clear-unread-count")
    public Result<Boolean> clearUserUnreadCount(
            @Parameter(description = "会话ID") @RequestParam String sessionId) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.clearUserUnreadCount(sessionId, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "设置会话置顶")
    @PostMapping("/set-top")
    public Result<Boolean> setSessionTop(
            @Parameter(description = "会话ID") @RequestParam String sessionId,
            @Parameter(description = "是否置顶") @RequestParam Boolean isTop) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.setSessionTop(sessionId, userId, isTop);
        
        return Result.success(result);
    }

    @Operation(summary = "设置会话免打扰")
    @PostMapping("/set-disturb-free")
    public Result<Boolean> setSessionDisturbFree(
            @Parameter(description = "会话ID") @RequestParam String sessionId,
            @Parameter(description = "是否免打扰") @RequestParam Boolean isDisturbFree) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.setSessionDisturbFree(sessionId, userId, isDisturbFree);
        
        return Result.success(result);
    }

    @Operation(summary = "删除会话")
    @PostMapping("/delete")
    public Result<Boolean> deleteSession(
            @Parameter(description = "会话ID") @RequestParam String sessionId) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.deleteSession(sessionId, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "恢复会话")
    @PostMapping("/restore")
    public Result<Boolean> restoreSession(
            @Parameter(description = "会话ID") @RequestParam String sessionId) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.restoreSession(sessionId, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "保存草稿")
    @PostMapping("/save-draft")
    public Result<Boolean> saveDraft(
            @Parameter(description = "会话ID") @RequestParam String sessionId,
            @Parameter(description = "草稿内容") @RequestParam String draftContent) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.saveDraft(sessionId, userId, draftContent);
        
        return Result.success(result);
    }

    @Operation(summary = "清除草稿")
    @PostMapping("/clear-draft")
    public Result<Boolean> clearDraft(
            @Parameter(description = "会话ID") @RequestParam String sessionId) {
        
        String userId = UserContext.getUserId();
        Boolean result = privateSessionService.clearDraft(sessionId, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "统计用户会话数量")
    @GetMapping("/count")
    public Result<Integer> countUserSessions() {
        String userId = UserContext.getUserId();
        Integer result = privateSessionService.countUserSessions(userId);
        
        return Result.success(result);
    }

    @Operation(summary = "获取用户总未读数")
    @GetMapping("/total-unread-count")
    public Result<Integer> getTotalUnreadCount() {
        String userId = UserContext.getUserId();
        Integer result = privateSessionService.getTotalUnreadCount(userId);
        
        return Result.success(result);
    }

    @Operation(summary = "清理无效会话")
    @PostMapping("/clean-invalid")
    public Result<Integer> cleanInvalidSessions(
            @Parameter(description = "保留天数") @RequestParam(defaultValue = "30") Integer days) {
        
        Integer result = privateSessionService.cleanInvalidSessions(days);
        
        return Result.success(result);
    }

    @Operation(summary = "获取会话统计信息")
    @GetMapping("/statistics")
    public Result<Object> getSessionStatistics(
            @Parameter(description = "开始时间") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) String endTime) {
        
        String userId = UserContext.getUserId();
        Object result = privateSessionService.getSessionStatistics(userId, startTime, endTime);
        
        return Result.success(result);
    }
}
